package com.vastcm.stuhealth.client.framework;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.vastcm.stuhealth.client.AppContext;
import com.vastcm.stuhealth.client.framework.report.ISqlService;

public class AppCache {
	private static AppCache instance = new AppCache();

	private Map<String, String> maxVersionMap = new HashMap<String, String>(10);

	private AppCache() {

	}

	public static AppCache getInstance() {
		return instance;
	}

	public String putMaxVersion(String itemFieldName, String maxVersion) {
		if (org.springframework.util.StringUtils.hasText(itemFieldName)) {
			return maxVersionMap.put(itemFieldName.toUpperCase(), maxVersion);
		}
		return null;
	}

	public String getMaxVersion(String itemFieldName) {
		if (org.springframework.util.StringUtils.hasText(itemFieldName)) {
			return maxVersionMap.get(itemFieldName.toUpperCase());
		}
		return null;
	}

	public static void initAppCache() {
		ISqlService sqlService = AppContext.getBean("sqlService", ISqlService.class);
		StringBuffer sql = new StringBuffer();
		sql.append("select 'SG' itemField, max(version) version from standard_height    \n");
		sql.append("union all                                                           \n");
		sql.append("select 'TZ' itemField, max(version) version from standard_weight    \n");
		sql.append("union all                                                           \n");
		sql.append("select 'XW' itemField, max(version) version from standard_Chest     \n");
		sql.append("union all                                                           \n");
		sql.append("select 'MB' itemField, max(version) version from standard_pulsation \n");
		sql.append("union all                                                           \n");
		sql.append("select 'FHL' itemField, max(version) version from standard_lungs    \n");
		sql.append("union all                                                           \n");
		sql.append("select 'YY' itemField, max(version) version from Standard_Nutrition \n");
		List rs = sqlService.query(sql.toString());
		for (int i = 0, size = rs.size(); i < size; i++) {
			Object[] datas = (Object[]) rs.get(i);
			String itemField = String.valueOf(datas[0]);
			String version = String.valueOf(datas[1]);
			AppCache.getInstance().putMaxVersion(itemField, version);
		}
	}
}